package com.contextor.magiccards.dao.utils;

import com.contextor.magiccards.model.user.User;
import org.springframework.util.StringUtils;

/**
 * User: sergey
 * Date: Mar 29, 2011 4:20:36 PM
 */
public abstract class ShardHelper {

    public final static int NUMBER_OF_SHARDS = 4;

    public static int getShard(User user) {
        if (user == null) {
            throw new RuntimeException("Invalid argument;");
        }
        return getShard(user.getUid());
    }

    public static int getShard(String uid) {
        if (!StringUtils.hasLength(uid)) {
            throw new RuntimeException("Invalid argument; uid is null or empty;");
        }
        int sum = 0;
        for (int i = 0; i < uid.length(); i++) {
            final char symbol = uid.charAt(i);
            sum += (int) symbol;
        }
        return sum % NUMBER_OF_SHARDS;
    }

    public static int getDefault() {
        return 0;
    }
}
